import Vue from 'vue'
import App from '@/App.vue'
import router from '@/router'
//导入组件库
import Vant from 'vant'
//导入axios
import axios from "axios"
//导入组件css文件
import 'vant/lib/index.css'
//绑定Vant
Vue.use(Vant)
//将axios绑定到原型中
Vue.prototype.$axios = axios
//基准路径
axios.defaults.baseURL = 'http://localhost:3000'
//axios响应拦截器
// axios.//axios设置请求拦截器
axios.interceptors.response.use(res => {
  // res：是每次拦截的结果，还没到达就拦截了
  const { message, statusCode } = res.data
  if (message == "用户信息验证失败" && statusCode == 401) {
    localStorage.removeItem('token')
    localStorage.removeItem('id')
    router.push("/login");

  }
  // this.$toast.fail(message);
  console.log(message, statusCode);
  return res


})




//添加路由守卫
router.beforeEach((to, from, next) => {
  const hastoken = localStorage.getItem("token")
  if (to.meta.auto) {
    if (hastoken) {
      return next()
    } else {
      return router.push("/login")
    }
  } else {
    next()
  }
})

//axios请求拦截器
axios.interceptors.request.use((config) => {
  if (!config.headers.Authorization && localStorage.getItem('token')) {
    config.headers.Authorization = localStorage.getItem('token')
  }
  return config
})




new Vue({
  router,
  render: function (h) { return h(App) }
}).$mount('#app')
